package cn.eoeit.alone.phoneImport.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import cn.eoeit.alone.phoneImport.R;
import cn.eoeit.alone.phoneImport.model.CityBean;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class CityBeanAdater extends BaseAdapter implements SectionIndexer 
{
	
	private ArrayList<CityBean> mList = null;
	private Context mContext= null;
	private LayoutInflater layoutInflater;
	
	//HashMap<String, Integer> pranva
	
	public CityBeanAdater(Context context ,ArrayList<CityBean> datas){
		mContext = context;
		layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(datas==null){
			datas=new ArrayList<CityBean>();
		}
		this.mList=datas;
	}

	 public void setDatas(ArrayList<CityBean> datas,boolean isAdd) {
			if(isAdd){
				mList.addAll(datas);
			}else{
				mList.clear();
				mList.addAll(datas);
			}
		}
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public CityBean getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override 
	public View getView(int position, View convertView, ViewGroup parent) {
		CityItemHolder holder = null;
		if(convertView == null )
		{
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.city_item, null); // mContext指的是调用的Activtty
			holder = new CityItemHolder();
			holder.txtName= (TextView) convertView.findViewById(R.id.name);
			holder.txtPhone= (TextView) convertView.findViewById(R.id.phone);
			holder.txtPost = (TextView) convertView.findViewById(R.id.post);
			holder.header = (LinearLayout) convertView.findViewById(R.id.section);
			convertView.setTag(holder);
		}else{
			holder = (CityItemHolder) convertView.getTag();
		}
		CityBean city=mList.get(position);
		String provance =city.getProvance();
        if (position == 0) {  
        	setSection(holder.header, provance);
        	holder.header.setVisibility(View.VISIBLE);  
        } else {  
            String preProvance = mList.get(position - 1).getProvance();
            if(provance.equals(preProvance)) {            
            	  setSection(holder.header, provance);            
            	  holder.header.setVisibility(View.VISIBLE);  
            } else {  
            	holder.header.setVisibility(View.GONE);  
            }  
        }  
        holder.txtName.setText(city.getCity());
		return convertView; 
	}
	
	private static class CityItemHolder{
		private TextView txtName;
		private TextView txtPost;
		private TextView txtPhone;
		private LinearLayout header;
	}

    
    public int getPositionForSection(int section) {  
        if (section == 35) {  
            return 0;  
        }  
        for (int i = 0; i < mList.size(); i++) {  
        	  String provance = mList.get(i).getProvance();  
//              char firstChar = l.toUpperCase().charAt(0);  
//              if (firstChar == section) {  
//                  return i;  
//              }  
        }  
        return -1;  
    }  
    public int getSectionForPosition(int arg0) {  
        return 0;  
    }  
    public Object[] getSections() {  
        return null;  
    }
    
    private void setSection(LinearLayout header, String label) {  
    	header.removeAllViews();
        TextView text = new TextView(mContext);  
        header.setBackgroundColor(0xffaabbcc);  
        text.setTextColor(Color.WHITE);  
        text.setText(label);  
        text.setTextSize(20);  
        text.setPadding(5, 0, 0, 0);  
        text.setGravity(Gravity.CENTER_VERTICAL);  
        header.addView(text);  
    }
}